﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MLM.Data;
using MLM.AdminCP;
using MLM.AdminCP.Class;

namespace MLM
{
    public static partial class MLM
    {
        public class Product
        {
            MLMDataContext _MLMDataContext = new MLMDataContext();

            public long Id { get; set; }
            public string Price { get; set; }
            public decimal PriceTax { get; set; }
            //public string Description { get; set; }
            public string Name { get; set; }
            public string Image { get; set; }
            public string Points { get; set; }
            public string VAT { get; set; }
            public Product(long id, long CountryID)
            {

                ProductBas _uitProducts = _MLMDataContext.ProductBases.FirstOrDefault(p => p.ProductBaseID == id);
                //long CountryID = long.Parse(_MLMDataContext.ProductInStocks.Where(p => p.ProductBaseID == id).FirstOrDefault().Stock.CountryID.ToString());
                long RoleID = Permission.GetCurrentRole();
                long UserID = Permission.GetCurrentUserId();
                var depo = _MLMDataContext.Users.FirstOrDefault(it => it.UserId == UserID);
                // GroupTax _GroupTax = _MLMDataContext.GroupTaxes.Where(p => p.CountryID == CountryID && p.RoleID == RoleID).FirstOrDefault();

                var query = _MLMDataContext.Users.Where(p => p.UserId == UserID);
                if (query.Count() > 0)
                {
                    long RankID = 0;
                    int? Bonus = 0;
                    try
                    {
                        RankID = long.Parse(query.FirstOrDefault().RankID.ToString());
                    }
                    catch (Exception)
                    {
                        RankID = 0;
                    }
                    try
                    {
                        Bonus = _MLMDataContext.Ranks.Where(p => p.RankID == RankID).FirstOrDefault().Bonus;

                    }
                    catch (Exception)
                    {
                        Bonus = 0;
                    }
                    try
                    {
                        if (_uitProducts.Points != null)
                        {
                            this.Points = long.Parse(_uitProducts.Points.ToString()) + "";
                        }
                        else
                        {
                            this.Points = "0";
                        }
                    }
                    catch (Exception)
                    {
                        this.Points = "0";
                    }

                }
                else {
                    this.Points = "0";
                }
                this.Id = id;
                //this.Description = _uitProducts.;
                this.Image = _uitProducts.ThumbImage;
                this.Name = _uitProducts.Name;
                //if (_GroupTax != null)
                //{
                this.Price = FormatPrice(_uitProducts.Price, 0, _MLMDataContext, depo.RoleID, id, depo.CountryID).ToString();
                this.PriceTax = (decimal)_uitProducts.Price + ((decimal)_uitProducts.Price) * ((decimal)CountryID.GetVATByCountryUser() / 100);
                this.VAT = CountryID.GetVATByCountryUser().ToString();
                //}
                //else
                //{
                //    this.Price = (decimal)_uitProducts.Price;
                //    this.VAT = "0";
                //}
            }
            public static decimal FormatPrice(object price, object tax, MLMDataContext ct, int? role, object productid = null, object countryid = null)
            {
                try
                {
                    if (productid != null && countryid != null && countryid != "" && productid != "")
                    {

                        long pId = long.Parse(productid.ToString());
                        long cId = long.Parse(countryid.ToString());
                        var pPrice =
                            ct.ProductPrices.FirstOrDefault(
                                it => it.ProductBaseID == pId && it.CountryID == cId && it.RoleID == role);
                        if (pPrice != null)
                        {
                            decimal _price = decimal.Parse(pPrice.Price.ToString());
                            decimal _tax = decimal.Parse(tax.ToString());
                            return (_price + _price * (_tax / 100));
                        }
                        else
                        {
                            decimal _price = decimal.Parse(price.ToString());
                            decimal _tax = decimal.Parse(tax.ToString());
                            return _price + _price * (_tax / 100);
                        }
                    }
                    else
                    {
                        decimal _price = decimal.Parse(price.ToString());
                        decimal _tax = decimal.Parse(tax.ToString());
                        return _price + _price * (_tax / 100);
                    }
                }
                catch (Exception ex)
                {
                    return 0;
                }
            }
        }
    }
}
